package com.dzl.goods.service.impl;

import com.dzl.base.pojo.BaseData;
import com.dzl.base.query.BaseQuery;
import com.dzl.base.service.BaseDataService;
import com.dzl.base.service.BaseServiceImpl;
import com.dzl.constant.CommonConstants;
import com.dzl.constant.WelfareGoodsEnum;
import com.dzl.goods.dao.WelfareGoodsClassifyEntryRelMapper;
import com.dzl.goods.pojo.WelfareGoodsClassifyEntryRel;
import com.dzl.goods.pojo.WelfareGoodsLabel;
import com.dzl.goods.pojo.vo.WelfareGoodsClassifyEntryRelVo;
import com.dzl.goods.pojo.vo.WelfareGoodsManageVo;
import com.dzl.goods.service.WelfareGoodsClassifyEntryRelService;
import com.dzl.goods.service.WelfareGoodsLabelRelService;
import com.dzl.goods.service.WelfareGoodsService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.ImageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 分类入口关联商品表(welfare_goods_classify_entry_rel)表服务实现类
 * @author liuguanjun
 * @date 2021-12-29 17:31:08
 */
@Service
public class WelfareGoodsClassifyEntryRelServiceImpl extends BaseServiceImpl<WelfareGoodsClassifyEntryRel> implements WelfareGoodsClassifyEntryRelService {

    @Autowired
    private WelfareGoodsClassifyEntryRelMapper mapper;
    @Autowired
    private BaseDataService baseDataService;
    @Autowired
    private WelfareGoodsLabelRelService welfareGoodsLabelRelService;
    @Autowired
    private WelfareGoodsService welfareGoodsService;

    @Override
    public PageInfo<WelfareGoodsClassifyEntryRelVo> getGoodsListClassifyEntryRelPageManager(BaseQuery query) {
        PageHelper.startPage(query);
        List<WelfareGoodsClassifyEntryRelVo> welfareGoodsWithLowPriceVoList = mapper.selectGoodsListClassifyEntryRelManager(query);
        if (EmptyUtils.isNotEmpty(welfareGoodsWithLowPriceVoList)) {
            welfareGoodsWithLowPriceVoList.forEach(welfareGoodsManageVo -> {
                if (EmptyUtils.isNotEmpty(welfareGoodsManageVo.getGoodsImg())) {
                    welfareGoodsManageVo.setGoodsImg(ImageUtils.setImageSrc(welfareGoodsManageVo.getGoodsImg()));
                }
                //查询商品最低价最高价等
                WelfareGoodsManageVo goodsPriceVo = welfareGoodsService.getGoodsPriceAndQuantityByGoodsId(welfareGoodsManageVo.getGoodsId());
                if (EmptyUtils.isNotEmpty(goodsPriceVo)) {
                    welfareGoodsManageVo.setLowerPrice(goodsPriceVo.getLowerPrice());
                    welfareGoodsManageVo.setUpperPrice(goodsPriceVo.getUpperPrice());
                    welfareGoodsManageVo.setLowerCostPrice(goodsPriceVo.getLowerCostPrice());
                    welfareGoodsManageVo.setUpperCostPrice(goodsPriceVo.getUpperCostPrice());
                    welfareGoodsManageVo.setGoodsRemainQuantity(goodsPriceVo.getGoodsRemainQuantity());
                }
                if(welfareGoodsManageVo.getGoodsStatus()== WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getCode()){
                    if(welfareGoodsManageVo.getIsShow()==CommonConstants.YES){
                        welfareGoodsManageVo.setGoodsStatusName("上架/不隐藏");
                    }else{
                        welfareGoodsManageVo.setGoodsStatusName("上架/已隐藏");
                    }
                }else{
                    welfareGoodsManageVo.setGoodsStatusName("下架");
                }
            });
        }
        PageInfo<WelfareGoodsClassifyEntryRelVo> page = new PageInfo<WelfareGoodsClassifyEntryRelVo>(welfareGoodsWithLowPriceVoList);
        return page;
    }

    @Override
    public PageInfo<WelfareGoodsClassifyEntryRelVo> getGoodsListClassifyEntryRelPageWeb(BaseQuery query) {
        PageHelper.startPage(query);
        List<WelfareGoodsClassifyEntryRelVo> welfareGoodsWithLowPriceVoList = mapper.selectGoodsListClassifyEntryRelWeb(query);
        if (EmptyUtils.isNotEmpty(welfareGoodsWithLowPriceVoList)) {
            welfareGoodsWithLowPriceVoList.forEach(welfareGoodsManageVo -> {
                if (EmptyUtils.isNotEmpty(welfareGoodsManageVo.getGoodsImg())) {
                    welfareGoodsManageVo.setGoodsImg(ImageUtils.setImageSrc(welfareGoodsManageVo.getGoodsImg()));
                }
                //查询商品最低价最高价等
                WelfareGoodsManageVo goodsPriceVo = welfareGoodsService.getGoodsPriceAndQuantityByGoodsId(welfareGoodsManageVo.getGoodsId());
                if (EmptyUtils.isNotEmpty(goodsPriceVo)) {
                    welfareGoodsManageVo.setLowerPrice(goodsPriceVo.getLowerPrice());
                    welfareGoodsManageVo.setUpperPrice(goodsPriceVo.getUpperPrice());
                    welfareGoodsManageVo.setLowerCostPrice(goodsPriceVo.getLowerCostPrice());
                    welfareGoodsManageVo.setUpperCostPrice(goodsPriceVo.getUpperCostPrice());
                    welfareGoodsManageVo.setGoodsRemainQuantity(goodsPriceVo.getGoodsRemainQuantity());
                }
                List<WelfareGoodsLabel> goodsLabelList = welfareGoodsLabelRelService.selectGoodsLabelListByGoodsId(welfareGoodsManageVo.getGoodsId(),2);
                if (EmptyUtils.isNotEmpty(goodsLabelList)) {
                    welfareGoodsManageVo.setGoodsLabelList(goodsLabelList);
                }else{
                    welfareGoodsManageVo.setGoodsLabelList(new ArrayList<>());
                }
            });
        }
        PageInfo<WelfareGoodsClassifyEntryRelVo> page = new PageInfo<WelfareGoodsClassifyEntryRelVo>(welfareGoodsWithLowPriceVoList);
        return page;
    }

    @Override
    public Integer setGoodsClassifyEntryImg(String imgUrl) {
        BaseData baseData =  baseDataService.selectByPrimaryKey(3);  //分类入口图片
        if (EmptyUtils.isEmpty(baseData)) {
            baseData = new BaseData();
            baseData.setStatus(CommonConstants.YES);
            baseData.setCreateTime(new Date());
            baseData.setName("分类入口图片地址");
            baseData.setValue("classifyEntryImg");
            baseData.setCode("classifyEntryImg");
            baseData.setRemark(EmptyUtils.isNotEmpty(imgUrl)?imgUrl:" ");
            baseData.setParentId(0);
            return baseDataService.insertSelective(baseData);
        }else{
            baseData.setUpdateTime(new Date());
            baseData.setRemark(EmptyUtils.isNotEmpty(imgUrl)?imgUrl:" ");
            return baseDataService.updateByPrimaryKeySelective(baseData);
        }
    }

    @Override
    public Integer setGoodsClassifyEntryName(String name) {
        BaseData baseData =  baseDataService.selectByPrimaryKey(2);  //分类入口名称
        if (EmptyUtils.isEmpty(baseData)) {
            baseData = new BaseData();
            baseData.setStatus(CommonConstants.YES);
            baseData.setCreateTime(new Date());
            baseData.setName("分类入口名称");
            baseData.setValue(name);
            baseData.setCode("classifyEntryName");
            baseData.setParentId(0);
            return baseDataService.insertSelective(baseData);
        }else{
            baseData.setValue(name);
            baseData.setUpdateTime(new Date());
            return baseDataService.updateByPrimaryKeySelective(baseData);
        }
    }

    @Override
    public Integer saveRelGoods(String goodsIds) {
        List<WelfareGoodsClassifyEntryRel> list = new ArrayList<>();
        String[] ids = goodsIds.split(",");
        for (String id:ids) {
            WelfareGoodsClassifyEntryRel welfareGoodsClassifyEntryRel = new WelfareGoodsClassifyEntryRel();
            welfareGoodsClassifyEntryRel.setGoodsId(Integer.valueOf(id));
            welfareGoodsClassifyEntryRel.setCreateTime(new Date());
            welfareGoodsClassifyEntryRel.setStatus(CommonConstants.YES);
            list.add(welfareGoodsClassifyEntryRel);
        }
        return mapper.insertList(list);
    }

    @Override
    public Integer deleteGoodsClassifyEntryById(Integer id) {
        WelfareGoodsClassifyEntryRel welfareGoodsClassifyEntryRel = new WelfareGoodsClassifyEntryRel();
        welfareGoodsClassifyEntryRel.setId(id);
        welfareGoodsClassifyEntryRel.setUpdateTime(new Date());
        welfareGoodsClassifyEntryRel.setStatus(CommonConstants.NO);
        Integer result =  mapper.updateByPrimaryKeySelective(welfareGoodsClassifyEntryRel);
        return result;
    }

    @Override
    public Map getGoodsClassifyEntryInfo() {
        Map map = Maps.newHashMapWithExpectedSize(2);
        BaseData baseData2 =  baseDataService.selectByPrimaryKey(2);  //分类入口名称
        BaseData baseData3 =  baseDataService.selectByPrimaryKey(3);  //分类入口图片
        if(EmptyUtils.isNotEmpty(baseData2) && EmptyUtils.isNotEmpty(baseData2.getValue())){
            map.put("name",baseData2.getValue());
        }else{
            map.put("name","");
        }
        if(EmptyUtils.isNotEmpty(baseData3) && EmptyUtils.isNotEmpty(baseData3.getRemark()) && EmptyUtils.isNotEmpty(baseData3.getRemark().trim())){
            map.put("img", ImageUtils.setImageSrc(baseData3.getRemark().trim()));
        }else{
            map.put("img","");
        }
        return map;
    }
}